﻿using System;
using System.Data;
using System.Text;
using ZHDJ.Data.Models.Center;
using ZHDJ.WebPage;
using ZHDJ.WebPage.Master;

namespace ZHDJ.Web.Portal.PhotoWall
{
    public partial class PhotoWallMorePage : UseMorePageMasterPage
    {
        /// <summary>
        /// 页数
        /// </summary>
        private string PageIndex
        {
            get
            {
                return this.Request["pageIndex"] ?? "";
            }
        }

        /// <summary>
        /// 每页条数
        /// </summary>
        private string PageSize
        {
            get
            {
                return this.Request["PageSize"] ?? "";
            }
        }

        private string BranchID
        {
            get
            {
                return this.Request["BranchID"] ?? "";
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            CheckLogin(true);

            if (!string.IsNullOrEmpty(this.PageIndex))
            {
                this.WriteList();
            }
            else
            {
                this.hfTotalCount.Value = PartyPhotoWall.GetPartyPhotoWallTitle(this.BranchID).Tables[1].Rows[0][0].ToString();
            }
        }

        private void WriteList()
        {
            StringBuilder sbHtml = new StringBuilder();
        
            DataTable dt = PartyPhotoWall.GetPartyPhotoWallData(Convert.ToInt32(this.PageIndex),Convert.ToInt32(PageSize), "", this.BranchID, "", "", "").Tables[0];

            for (int i = 0; i < dt.Rows.Count; i++)
            {
                sbHtml.Append("<li>");
                sbHtml.AppendFormat("<a href=\"PhotoWallPage.aspx?PhotoWallID={0}\" target=\"_blank\" title=\"{1}\">", dt.Rows[i]["ID"], dt.Rows[i]["Title"]);
                sbHtml.AppendFormat("<img src=\"{0}\" alt=\"\" />", UpFileBusiness.GetFileUrl(PartyPhotoWall.QueryFirstImageUrl(dt.Rows[i]["ID"].ToString())));
                sbHtml.AppendFormat("<span>{0}</span>", GetShortString(dt.Rows[i]["Title"], 10));
                sbHtml.Append("</a>");
                sbHtml.Append("</li>");
            }

            Response.Clear();
            Response.Write(sbHtml.ToString());
            Response.End();
        }

        private static string GetShortString(object obj, int length)
        {
            if (obj != null)
                if (obj.ToString().Length > length)
                    return obj.ToString().Substring(0, (length - 2)) + "...";
                else
                    return obj.ToString();
            else
                return "";
        }
    }
}